﻿using HK.Core.Common;
using HK.Editor.GUIEx.Protocol.Drag;
using UnityEngine;

namespace HK.Editor.GUIEx.Drag
{
    
    /// <summary>
    /// 拖拽事件代理
    /// <para>* 可重排序列表(ReorderableList)中拖拽事件的代理</para>
    /// <para>* 用以回传可重排序列表在Inspector视图面板上的拖拽事件</para>
    /// <para>* 拖拽目标分为，Unity内部对象(如GameObject等),文件/问价夹等Unity资产对象等。</para>
    /// </summary>
    public class DragAndDropSurrogate : ClassExtension, IDragAndDropSurrogate
    {

#region Create
        
        /// <summary>
        /// 创建拖拽代理
        /// </summary>
        /// <param name="iTargetType">目标类型</param>
        /// <returns>拖拽代理</returns>
        public static DragAndDropSurrogate Create(System.Type iTargetType)
        {
            return new DragAndDropSurrogate
            {
                TargetType = iTargetType,
                _enabled = true,
                ExactType = false
            };
        }
        
        /// <summary>
        /// 创建拖拽代理
        /// </summary>
        /// <param name="iTargetType">目标类型</param>
        /// <param name="iOnDragAndDropDown">拖拽事件回调</param>
        /// <returns>拖拽代理</returns>
        public static DragAndDropSurrogate Create(System.Type iTargetType, ObjectsDragAndDropDown iOnDragAndDropDown)
        {
            return new DragAndDropSurrogate
            {
                TargetType = iTargetType, 
                _onObjectsDragAndDropDown = iOnDragAndDropDown,
                _enabled = true,
                ExactType = false
            };
        }
        
        /// <summary>
        /// 创建拖拽代理
        /// </summary>
        /// <param name="iTargetType">目标类型</param>
        /// <param name="iOnDragAndDropDown">拖拽事件回调</param>
        /// <returns>拖拽代理</returns>
        public static DragAndDropSurrogate Create(System.Type iTargetType, AssetsDragAndDropDown iOnDragAndDropDown)
        {
            return new DragAndDropSurrogate
            {
                TargetType = iTargetType, 
                _onAssetsDragAndDropDown = iOnDragAndDropDown,
                _enabled = true,
                ExactType = false
            };
        }

#endregion

        /// <summary>
        /// 保护构造函数
        /// </summary>
        protected DragAndDropSurrogate() {}

        /// <summary>
        /// 目标类型
        /// </summary>
        public System.Type TargetType { get; private set; } = null;
        
        /// <summary>
        /// 扩展类型
        /// </summary>
        public bool ExactType { get; private set; } = false;
        
        /// <summary>
        /// Unity对象拖拽事件
        /// </summary>
        private ObjectsDragAndDropDown _onObjectsDragAndDropDown = null;
        
        /// <summary>
        /// 文件/文件夹拖拽事件
        /// </summary>
        private AssetsDragAndDropDown _onAssetsDragAndDropDown = null;

        private bool _enabled = false;

        /// <summary>
        /// 有效标志位
        /// </summary>
        public bool Valid => _enabled && null != TargetType;

        /// <summary>
        /// 拖拽
        /// </summary>
        /// <param name="iObjectReference">拖拽的引用对象列表</param>
        public void DragAndDropDown(Object iObjectReference) 
        {
            if (null != _onObjectsDragAndDropDown) 
            {
                _onObjectsDragAndDropDown.Invoke(iObjectReference);
            }
        }
        
        /// <summary>
        /// 拖拽
        /// </summary>
        /// <param name="iAssets">拖拽的资产列表(如:文件/文件夹等)</param>
        public void DragAndDropDown(string[] iAssets) 
        {
            if (null != _onAssetsDragAndDropDown) 
            {
                _onAssetsDragAndDropDown.Invoke(iAssets);
            }
        }
    }
}
